package top.infopub.mgr.cap.web;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import top.infopub.mgr.cap.domain.FinancingDomain;
import top.infopub.mgr.cap.domain.LookInfoDomain;
import top.infopub.mgr.cap.dto.LookInfoBeginDto;
import top.infopub.mgr.cap.jsonvo.LookInfoBeginJsonVo;
import top.infopub.mgr.cap.jsonvo.LookInfoJsonVo;
import top.infopub.mgr.cap.service.CreditBalanceManagementService;
import top.infopub.mgr.cap.service.LookInfoBeginService;
import top.infopub.mgr.cap.vo.LookInfoBeginVo;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.model.mkt.ProjectAttachment;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 融资初调管理
 * @author wuzhiyang
 * @version 2020年1月10日
 * @see LookInfoController
 * @since
 */
@Controller
@RequestMapping("/lookInfoBegin")
public class LookInfoBeginController extends GenericFlowController {

    @Autowired
    private LookInfoBeginService lookInfoBeginService;

    @Autowired
    private CreditBalanceManagementService creditBalanceManagementService;

    @RequestMapping("/toDealOrDetail")
    public ModelAndView queryById(HttpServletRequest request, String id) {
        request.setAttribute("id",id);
        return new ModelAndView("cap/lay-lookInfoBegin-detail");
    }


    /**
     * 融资初调列表数据
     * @return
     */
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<LookInfoDomain> queryDataByCondition(LookInfoBeginVo lookInfoBeginVo, ParamPage paramPage) {
        Page<LookInfoDomain> page = new Page<LookInfoDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        page = lookInfoBeginService.selectByDomainWhere(page, lookInfoBeginVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
     * 融资初调列表所需数据
     * @return
     */
    @RequestMapping("/queryDrop")
    @ResponseBody
    public RspVo queryDrop(){
        LookInfoJsonVo lookInfoJsonVo = new LookInfoJsonVo();
       List<String>  bank = creditBalanceManagementService.selectBankName();
       lookInfoJsonVo.setBankName(bank);
        return RspVo.getSuccessResponse(lookInfoJsonVo);
    }

    /**
     * 融资初调详情
     */
    @RequestMapping("/queryLookInfoBegin")
    @ResponseBody
    public RspVo queryLookInfoBegin(String id) {
        LookInfoBeginDto lookInfoDto = lookInfoBeginService.queryDetailById(id);
        return RspVo.getSuccessResponse(lookInfoDto);
    }

    /**
     * 融资初调保存
     */
    @RequestMapping("/save")
    @ResponseBody
    public RspVo save(String bodyStr,Integer submitType) {
        AuthorizationVO userDtl = SessionUserUtils.getSessionAttributeForUserDtl();
        String s = StringEscapeUtils.unescapeJava(bodyStr);
        LookInfoBeginJsonVo vo = JSON.parseObject(s, LookInfoBeginJsonVo.class);
        /**
         * 附件处理
         * */
        List<ProjectAttachment> attachmentList = new ArrayList<>();
        if(vo.getFileAtt()!=null){
            String[] fileAtt = vo.getFileAtt();
            for (String file : fileAtt) {
                ProjectAttachment attachment = createProjectFileAtta(userDtl,vo.getLookInfoDto().getProCode(),file);
                attachmentList.add(attachment);
            }
        }
        return lookInfoBeginService.saveAll(vo,userDtl,attachmentList,submitType);
    }

    /**
     * 融资台账列表数据
     * @return
     */
    @RequestMapping("/queryFinancingByCondition")
    @ResponseBody
    public RspVo<FinancingDomain> queryFinancingByCondition(LookInfoBeginVo lookInfoBeginVo, ParamPage paramPage) {
        Page<FinancingDomain> page = new Page<FinancingDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        page = lookInfoBeginService.selectFinByDomainWhere(page, lookInfoBeginVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }
}
